﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using 飞机大战.Properties;

namespace 飞机大战
{
    class PlaneEnemy : PlaneFather
    {
        static Random r = new();

        /// <summary>
        /// 最小的飞机
        /// </summary>
        private static Image img1 = Resources.enemy0;

        /// <summary>
        /// 中间的飞机
        /// </summary>
        private static Image img2 = Resources.enemy1;

        /// <summary>
        /// 最大的飞机
        /// </summary>
        private static Image img3 = Resources.enemy2;

        /// <summary>
        /// 因为每一种飞机的大小、生命值、速度都不一样，所以需要声明一个标识来标记敌机类型：0-最小的飞机，1-中间的飞机，2-最大的飞机
        /// </summary>
        public int EnemyType { get; set; }

        //根据敌机类型，分别写三个函数，用于返回飞机的图片、速度、生命值

        /// <summary>
        /// 返回对应图片
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Image GetImage(int type)
        {
            switch (type)
            {
                case 0: return img1;
                case 1: return img2;
                case 2: return img3;
            }
            return null;
        }

        /// <summary>
        /// 返回对应生命值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int GetLife(int type)
        {
            switch (type)
            {
                case 0:
                    return 1;
                case 1:
                    return 2;
                case 2:
                    return 3;
            }
            return 0;
        }

        /// <summary>
        /// 返回对应速度
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int GetSpeed(int type)
        {
            switch (type)
            {
                case 0:
                    return 7;
                case 1:
                    return 6;
                case 2: return 5;
            }
            return 0;
        }

        public PlaneEnemy(int x, int y, int type) : base(x, y, GetImage(type), GetSpeed(type), GetLife(type), Direction.down)
        {
            this.EnemyType = type;
        }

        /// <summary>
        /// 重写父类中的Draw函数，绘制敌机
        /// </summary>
        /// <param name="g"></param>
        public override void Draw(Graphics g)
        {
            //随着将敌机绘制出来，让其开始移动
            this.Move();

            //根据不同的敌机类型绘制
            switch (this.EnemyType)
            {
                case 0:
                    g.DrawImage(img1, this.X, this.Y);
                    break;
                case 1:
                    g.DrawImage(img2, this.X, this.Y);
                    break;
                case 2:
                    g.DrawImage(img3, this.X, this.Y);
                    break;
            }
        }

        /// <summary>
        /// 重写父类中的Move函数
        /// </summary>
        public override void Move()
        {
            //根据游戏对象的方向进行移动
            switch (this.dir)
            {
                case Direction.up:
                    this.Y -= this.Speed;
                    break;
                case Direction.down:
                    this.Y += this.Speed;
                    break;
                case Direction.left:
                    this.X -= this.Speed;
                    break;
                case Direction.right:
                    this.X += this.Speed;
                    break;
            }

            if (this.X <= 0)
            {
                this.X = 0;
            }
            if (this.X >= 400)
            {
                this.X = 400;
            }
            if (this.Y <= 0)
            {
                this.Y = 0;
            }
            if (this.Y >= 800)
            {
                //到达窗体底端时，让敌机离开窗体
                this.Y = 1400;

                //离开窗体的同时，销毁当前敌机
                SingleObject.GetSingle().RemoveGameObject(this);
            }

            //小敌机纵坐标大于等于某个值后，加快速度
            if (this.EnemyType == 0 && this.Y >= 400)
            {
                this.Speed += 1;
            }

            //百分之五的概率发射子弹
            if (r.Next(0, 100) > 95)
            {
                Fire();
            }
        }

        /// <summary>
        /// 敌机发射子弹
        /// </summary>
        public void Fire()
        {
            SingleObject.GetSingle().AddGameObject(new EnemyBullet(this, 11, 1));
        }

        /// <summary>
        /// 判断敌人是否死亡
        /// </summary>
        public override void IsOver()
        {
            //敌机坠毁
            if (this.Life <= 0)
            {
                //将其从游戏中移除
                SingleObject.GetSingle().RemoveGameObject(this);

                //播放敌机爆炸的图片
                SingleObject.GetSingle().AddGameObject(new EnemyBoom(this.X, this.Y, this.EnemyType));

                //敌机爆炸，根据敌机类型给玩家加分
                switch (this.EnemyType)
                {
                    case 0:
                        SingleObject.GetSingle().Score += 100;
                        break;
                    case 1:
                        SingleObject.GetSingle().Score += 200;
                        break;
                    case 2:
                        SingleObject.GetSingle().Score += 300;
                        break;
                }
            }
        }
    }
}

